﻿using AppEszköz.ErrorControl;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace AppEszköz.Trace
{
    public class SqlTraceListener : TraceListener
    {
        private string sqlConnectionString = string.Empty;
        private string storedProcedureName = "fm_WriteTrace";

        public string SqlConnectionString
        {
            get
            {
                return this.sqlConnectionString;
            }
            set
            {
                this.sqlConnectionString = value;
            }
        }

        public string StoredProcedureName
        {
            get
            {
                return this.storedProcedureName;
            }
            set
            {
                this.storedProcedureName = value;
            }
        }

        public SqlTraceListener()
        {
        }

        public SqlTraceListener(string connString)
        {
            this.sqlConnectionString = connString;
        }

        public SqlTraceListener(string connString, string storedProcedureName)
        {
            this.sqlConnectionString = connString;
            this.storedProcedureName = storedProcedureName;
        }

        public override void Write(string message)
        {
            this.Write(message, "NA");
        }

        public override void Write(string message, string category)
        {
            if (new TraceSwitch("TraceLevelSwitch", "Trace Level for Entire Application").Level <= TraceLevel.Off)
                return;
            SqlConnection connection = new SqlConnection(this.sqlConnectionString);
            //SqlParameterBuilder preambleParameters = this.getPreambleParameters();
            //if (message.Length > 5000)
            //    message = message.Substring(0, 5000);
            //if (category.Length > 100)
            //    category = category.Substring(0, 100);
            //preambleParameters.AddParameter("@eventDate", SqlDbType.DateTime, (object)DateTime.Now);
            //preambleParameters.AddParameter("@message", SqlDbType.VarChar, (object)message);
            //preambleParameters.AddParameter("@category", SqlDbType.VarChar, (object)category);
            //try
            //{
            //    SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, this.storedProcedureName, preambleParameters.ToArray());
            //}
            //catch (Exception ex)
            //{
            //    throw new GeneralException("SQLTraceListener - Unable to execute SQL SP", ex);
            //}
            //finally
            //{
            //    if (connection != null)
            //    {
            //        connection.Close();
            //        connection.Dispose();
            //    }
            //}
        }

        public override void WriteLine(string message)
        {
            this.Write(message);
        }

        public override void WriteLine(string message, string category)
        {
            this.Write(message, category);
        }

        //private SqlParameterBuilder getPreambleParameters()
        //{
        //    SqlParameterBuilder parameterBuilder = new SqlParameterBuilder();
        //    StackTrace stackTrace = new StackTrace();
        //    int index1 = 0;
        //    string str1 = MethodBase.GetCurrentMethod().ReflectedType.ToString();
        //    MethodBase method;
        //    string str2;
        //    do
        //    {
        //        ++index1;
        //        method = stackTrace.GetFrame(index1).GetMethod();
        //        str2 = method.ReflectedType.FullName;
        //    }
        //    while (str2.StartsWith("System") || str2.EndsWith(str1));
        //    StringBuilder stringBuilder = new StringBuilder();
        //    ParameterInfo[] parameters = method.GetParameters();
        //    for (int index2 = 0; index2 < parameters.Length; ++index2)
        //    {
        //        stringBuilder.Append("ParameterName=");
        //        stringBuilder.Append(parameters[index2].Name);
        //        stringBuilder.Append(" ParameterType=");
        //        stringBuilder.Append(parameters[index2].ParameterType.Name);
        //        stringBuilder.Append("    ");
        //        stringBuilder.Append(Environment.NewLine);
        //    }
        //    if (str2.Length > 200)
        //        str2 = str2.Substring(0, 200);
        //    string str3 = method.Name.Length <= 200 ? method.Name : method.Name.Substring(0, 200);
        //    if (stringBuilder.Length > 3000)
        //        stringBuilder.Length = 3000;
        //    parameterBuilder.AddParameter("@class", SqlDbType.VarChar, (object)str2);
        //    parameterBuilder.AddParameter("@method", SqlDbType.VarChar, (object)str3);
        //    parameterBuilder.AddParameter("@parameters", SqlDbType.VarChar, (object)stringBuilder.ToString());
        //    return parameterBuilder;
        //}
    }
}